/*
 * Created by JFormDesigner on Wed Apr 18 15:49:22 EEST 2012
 */

package tr.gov.tubitak.bilgem.esya.crlviewer;

import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * @author Haydar Karabas
 */
public class ECRLViewer extends JFrame {
    public ECRLViewer() {
        initComponents();
        loadWindowIcon();
    }

    private static void setLookAndFeel() {
        try {
            UIManager.setLookAndFeel(new NimbusLookAndFeel());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void initLocale() {
        try {
            Locale trLocale = new Locale("en","US");
            Locale.setDefault(trLocale);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    void loadWindowIcon()
    {
        ImageIcon retImageIcon=null;
        try
        {
            retImageIcon =new ImageIcon(getClass().getResource("/tr/gov/tubitak/bilgem/esya/crlviewer/images/crl.png"));
            setIconImage(retImageIcon.getImage());
        }
        catch (Exception e)
        {
        }
    }

    private static void createView(File file)
    {
        FileInputStream fis = null;
        byte [] crlContent = null;
        try {
            fis = new FileInputStream(file);
            int availableCount = fis.available();
            crlContent =new byte[availableCount];
            fis.read(crlContent);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        ECRLDialog.showCRL(crlContent);
    }


    public static void main(String[] args)
    {
        setLookAndFeel();
        initLocale();
        File file=null;
        if(args.length>0)
        {
            file = new File(args[0]);
        }

        if(file==null)
        {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setDialogTitle(Bundle.getString("ECRLViewer.silDosyaSec"));
            FileNameExtensionFilter filter = new FileNameExtensionFilter(
                    Bundle.getString("ECRLViewer.silDosyalari"), "crl");
            fileChooser.setFileFilter(filter);
            int returnVal = fileChooser.showOpenDialog(null);
            if(returnVal == JFileChooser.APPROVE_OPTION) {
                file = fileChooser.getSelectedFile();
            }
        }
        if(file!=null)
        {
            final File finalFile = file;
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    createView(finalFile);
                }
            });
        }
        return;
    }

    void loadCrl(File crlFile){
        FileInputStream fis = null;
        byte [] crlContent = null;
        try {
            fis = new FileInputStream(crlFile);
            int availableCount = fis.available();
            crlContent =new byte[availableCount];
            fis.read(crlContent);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        ECRLDialog.showCRL(crlContent);
         //eCRLPanel1.loadCrl(crlContent);
       // eCRLPanel1.loadCrl(crlFile);
    }


    private void okButtonActionPerformed(ActionEvent e) {
        setVisible(false);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        ResourceBundle bundle = ResourceBundle.getBundle("tr.gov.tubitak.bilgem.esya.crlviewer.lang");
        dialogPane = new JPanel();
        contentPanel = new JPanel();
        eCRLPanel1 = new ECRLPanel();
        buttonBar = new JPanel();
        okButton = new JButton();

        //======== this ========
        setTitle(bundle.getString("ECRLViewer.this.title"));
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== dialogPane ========
        {
            dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
            dialogPane.setLayout(new BorderLayout());

            //======== contentPanel ========
            {
                contentPanel.setLayout(new GridBagLayout());
                ((GridBagLayout)contentPanel.getLayout()).columnWidths = new int[] {0, 0};
                ((GridBagLayout)contentPanel.getLayout()).rowHeights = new int[] {0, 0};
                ((GridBagLayout)contentPanel.getLayout()).columnWeights = new double[] {1.0, 1.0E-4};
                ((GridBagLayout)contentPanel.getLayout()).rowWeights = new double[] {1.0, 1.0E-4};
                contentPanel.add(eCRLPanel1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 0), 0, 0));
            }
            dialogPane.add(contentPanel, BorderLayout.CENTER);

            //======== buttonBar ========
            {
                buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
                buttonBar.setLayout(new GridBagLayout());
                ((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 80};
                ((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0};

                //---- okButton ----
                okButton.setText(bundle.getString("ECRLViewer.okButton.text"));
                okButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        okButtonActionPerformed(e);
                    }
                });
                buttonBar.add(okButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 0), 0, 0));
            }
            dialogPane.add(buttonBar, BorderLayout.SOUTH);
        }
        contentPane.add(dialogPane, BorderLayout.CENTER);
        setSize(515, 545);
        setLocationRelativeTo(null);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel dialogPane;
    private JPanel contentPanel;
    private ECRLPanel eCRLPanel1;
    private JPanel buttonBar;
    private JButton okButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
